<?php

namespace app\index\controller;

use app\index\job\JobActivity;
use think\Cache;
use think\Controller;
use think\Db;
use think\Loader;
use think\Queue;
use think\Request;
use think\Validate;

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");                // 允许的方法
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");  // 允许的请求头
header("Access-Control-Expose-Headers: Authorization");                                 // 允许客户端获取的响应头

class Api extends Controller
{
    /**
     * 套餐列表
     * */
    public function mealList()
    {
        $list = Db::name('meal')->where('status', 1)->order('id desc')->select();
        if ($list) {
            foreach ($list as $k => &$v) {
                $index = $k % 5;

                $borderColor = '';
                switch ($index) {
                    case 0:
                        $borderColor = '#60A5FA';
                        break;
                    case 1:
                        $borderColor = '#2DD4BF';
                        break;
                    case 2:
                        $borderColor = '#818CF8';
                        break;
                    case 3:
                        $borderColor = '#C084FC';
                        break;
                    case 4:
                        $borderColor = '#F472B6';
                        break;
                }
                $v['borderColor'] = $borderColor;

                $v['img'] = getHost() . $v['img'];
            }
            apiReturn(1, '获取数据成功', $list);
        } else {
            apiReturn(2, '获取数据失败');
        }
    }

    /**
     * 套餐详情
     * */
    public function mealDetail()
    {
        $id = input('id');

        if (empty($id)) apiReturn(2, '参数错误');

        $info = Db::name('meal')->where(['id' => $id])->find();
        if (empty($info)) apiReturn(2, '套餐不存在');

        $info['tags'] = doTags($info['tags']);

        $video_list = Db::name('meal_video')->where(['meal_id' => $id, 'status' => 1])->order('id desc')->select();
        if ($video_list) {
            foreach ($video_list as &$v) {
                $v['path'] = getHost() . $v['path'];
                $v['zan_count'] = numberFormatShort($v['zan_count'] + $v['user_zan_count']);
            }
        }

        $author = Db::name('author')->where(['id' => 1])->find();
        if ($author) {
            $author['img'] = getHost() . $author['img'];
            $author['fens_count'] = numberFormatShort($author['fens_count']);
        }

        $meal_list = Db::name('meal')->where(['id' => ['neq', $id]])->orderRaw('rand()')->limit(2)->select();
        if ($meal_list) {
            foreach ($meal_list as &$v) {
                $v['img'] = getHost() . $v['img'];
            }
        }

        $data = [
            'info' => $info,
            'video_list' => $video_list,
            'author' => $author,
            'meal_list' => $meal_list
        ];

        apiReturn(1, '获取数据成功', $data);
    }

    /**
     * 点赞
     * */
    public function zan()
    {
        $id = input('id');

        if (empty($id)) apiReturn(2, '参数错误');

        $info = Db::name('meal_video')->where(['id' => $id])->find();
        if (empty($info)) apiReturn(2, '视频不存在');

        $res = Db::name('meal_video')->where(['id' => $id])->setInc('user_zan_count');

        if ($res) {
            $new_info = Db::name('meal_video')->where(['id' => $id])->find();
            apiReturn(1, '点赞成功', numberFormatShort($new_info['zan_count'] + $new_info['user_zan_count']));
        } else {
            apiReturn(2, '点赞失败');
        }

    }
}
